// pages/mall/paySuccess/paySuccess.js
import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
const http = new Http()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showRedPackage: 1,
    isSuccess: 0,
    orderNo: '',
    hongbaoId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isSuccess: options.success
    })
    if (options.orderNo != undefined) {
      this.setData({
        orderNo: options.orderNo
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.redPackageDialog = this.selectComponent("#redPackageDialog")
    if(this.data.isSuccess == 1){
      this.redPackageDialog.show()
    }
    if (this.data.orderNo.length > 0) {
      this.getRedPackage()
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    return {
      title: '【融生活】恭喜获得优惠券红包，赶快分享领取吧！',
      path: '/pages/mall/redPackage/redPackage?hongbaoId=' + this.data.hongbaoId,
      imageUrl: 'https://media.links2all.com/Ft17-5yRJPmMBJH9uay2laMwGFzm',
      success: (res) => {
        if (res.errMsg == 'shareAppMessage:ok') {
          that.setData({
            showRedPackage: 0
          })
        }
      },
      fail: (res) => {
        
      }
    }
  },

  /**
   * 自定义方法
   */
  goMallIndex: function () {
    wx.switchTab({
      url: '/pages/mall/index/index',
    })
  },

  repay: function(){
    wx.redirectTo({
      url: "/pages/me/order/order?currentTab=0"
    })
  },

  goOrder: function () {
    wx.redirectTo({
      url: '/pages/me/order/order?currentTab=1',
    })
  },

  getRedPackage: function () {
    var orderNo = this.data.orderNo
    var that = this
    http.sendHttp(Api.get_redpackage, {
      data: {
        orderNo: orderNo
      },
      success: function (res) {
        that.setData({
          hongbaoId: res.data.obj.hongbaoId
        })
      }    
    })
  }
})